package com.gjm.demo;

import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

@RequestMapping("/request")
@RestController
public class RequestController {

    @RequestMapping("/m1")
    public String test() {
        return "测试代码";
    }

    //传递Map数据
    @RequestMapping("/m2")
    public String mapTest(@RequestBody HashMap<String, Integer> hashMap) {
        return hashMap.toString();
    }

    //传递数组数据
    @RequestMapping("/m3")
    public String arrTest(int[] arr) {
        return Arrays.toString(arr);
    }

    //传递list数据
    @RequestMapping("/m4")
    public String listTest(@RequestParam List<Integer> list) {
        return list.toString();
    }

    //传递json数据
    @RequestMapping("/m5")
    public String transmitJson(@RequestBody UserInfo userInfo) {
        return userInfo.toString();
    }

//    //从URL中获取参数
//    @RequestMapping("/article/{articleType}/{articleID}")
//    public String getData1(@PathVariable Integer articleID, @PathVariable String articleType) {
//        return articleType + ", " + articleID;
//    }

    //从URL中获取参数
    @RequestMapping("/article/{Type}/{ID}")
    public String getData2(@PathVariable("ID") Integer articleID,
                          @PathVariable("Type") String articleType) {
        return articleType + ", " + articleID;
    }

    //上传文件
    @RequestMapping("/m6")
    public String transmitFile(@RequestPart MultipartFile file) {
        return file.getOriginalFilename();//返回原始名
//        return file.getName();//返回参数名
    }

    //获取cookie
    @RequestMapping("/m7")
    public String getCookies1(HttpServletRequest request) {
        Cookie[] cookies = request.getCookies();
        if (cookies != null) {
            for (Cookie cookie : cookies) {
                System.out.println(cookie.getName() + ": " + cookie.getValue());
            }
            return "返回Cookie成功";
        }
        return "返回Cookie失败";
    }

    @RequestMapping("/m8")
    public String getCookies2(@CookieValue("class") String classID) {
        return classID;
    }

    @RequestMapping("/m9")
    public String getCookies3(@CookieValue("class") String classID, @CookieValue("name") String name) {
        return classID + ", " + name;
    }

    @RequestMapping("/m10")
    public String setSession(HttpServletRequest request) {
        HttpSession session = request.getSession(true);
        session.setAttribute("userName", "张三");
        session.setAttribute("age", 17);
        return "session设置成功";
    }

    //获取session
    @RequestMapping("/m11")
    public String getSession1(HttpServletRequest request) {
        HttpSession session = request.getSession(false);
        //true: 若没有获取到session，就返回空的session对象
        //false: 若没有获取到session，就返回null
        if (session == null) {
            return "用户未登录";
        } else {
            String userName = (String)session.getAttribute("userName");
            return userName + "已登录";
        }
    }

    @RequestMapping("/m12")
    public String getSession2(HttpSession session) {
         String userName = (String)session.getAttribute("userName");
         return userName + "登录";
    }

    //获取header中的信息
    @RequestMapping("/m13")
    public String getHeader1(HttpServletRequest request) {
        String userAgent = request.getHeader("User-Agent");
        return userAgent;
    }

    @RequestMapping("/m14")
    public String getHeader2(@RequestHeader("User-Agent") String userAgent) {
        return userAgent;
    }
}
